package com.znsd.zl.dict.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znsd.zl.dict.dao.DictItemDao;
import com.znsd.zl.dict.entity.DictItemEntity;
import com.znsd.zl.dict.service.IDictItemService;
import com.znsd.zl.dict.utils.ConditionPageUtils;
import com.znsd.zl.dict.utils.JsonUtils;
import com.znsd.zl.dict.utils.RedisTemplateUtils;
import com.znsd.zl.dict.utils.SaveCacheKey;
import com.znsd.zl.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DictDao
 * @Description TODO
 * @Author zhubin
 * @Date 2020/8/17 19:52
 * @Version v1.0
 */
@Service
public class DictItemServiceImpl extends ServiceImpl<DictItemDao, DictItemEntity> implements IDictItemService {

    @Autowired
    RedisTemplateUtils redisTemplateUtils;

    @Override
    public boolean save(DictItemEntity entity) {
        Integer checkNum = this.baseMapper.checkDictItemValue(entity.getDictId(), entity.getDictItemValue());
        if (checkNum <= 0) {
            int row = this.baseMapper.insert(entity);
            return row == 1;
        }
        return false;
    }

    /**
     *  通过map动态的进行条件查询
     * @param params 封装的查询条件
     * @return 返回封装的分页对象
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return ConditionPageUtils.conditionPage(params,this.getBaseMapper());
    }

    /**
     * 根据字典类型查询所有字典数据
     * @param dictType 字典类型
     * @return  封装的对象
     */
    @Override
    public List<DictItemEntity> queryTextByDictType(String dictType) {
        // 获取缓存
        String cacheKey = "";
        try {
            cacheKey = redisTemplateUtils.get(dictType);
        } catch (Exception e) {
            e.printStackTrace();
            List<DictItemEntity> dictItems = this.baseMapper.queryTextByDictType(dictType);
            return dictItems;
        }

        if (StringUtils.isEmpty(cacheKey)) {
            List<DictItemEntity> dictItems = this.baseMapper.queryTextByDictType(dictType);
            // 保存key的名称
            SaveCacheKey.save(dictType);
            // 设置2分钟过期
            redisTemplateUtils.set(dictType, JsonUtils.objectToJson(dictItems),120);
            return dictItems;
        }
        List dictItems = JsonUtils.jsonToObject(cacheKey, List.class);
        return dictItems;
    }

    /**
     * 根据字典类型和字典数据值 精确查询单个文本
     * @param dictType 字典类型
     * @param itemValue 字典数据值
     * @return 查询的单个对象
     */
    @Override
    public DictItemEntity queryTextByDictTypeAndValue(String dictType, String itemValue) {
        return this.baseMapper.queryTextByDictTypeAndValue(dictType,itemValue);
    }

    @Override
    public boolean updateById(DictItemEntity entity) {
        Integer checkNum = this.baseMapper.checkDictItemValue(entity.getDictId(), entity.getDictItemValue());
        if (checkNum <= 0) {
            int row = this.baseMapper.updateById(entity);
            return row == 1;
        }
        return false;
    }
}
